﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using NCT.Framework;
using NCT.Framework.Database;
using VISS.API.CustomUtils;



namespace VISS.API.Users
{

    class UserPassCodeBC : IUserPassCode
    {

        #region IUserPassCode Members

        /// <summary>
        /// Inserts the specified item.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <returns></returns>
        public bool Insert(UserPassCode item)
        {
            bool result = true;
            DbConnection dbCon = null;
            DbTransaction dbTrans = null;
            try
            {
                //Get connection name
                string connectionName = ConfigInfo.ConnectionName;

                //Create connection & transaction
                dbCon = Database.CreateConnection(connectionName, true);
                dbTrans = dbCon.BeginTransaction();

                result = UserPassCodeDA.Insert(item, connectionName, dbCon, dbTrans) > 0;
            }


            catch (Exception ex)
            {
                LogUtils.Error(string.Format("VISS.Users.UserPassCodeBC.Insert({0}, {1}, {2}): \n" + ex.ToString(),
                                    SerializerUtils.JsonSerialize(item)));
                result = false;
            }
            finally
            {
                if (dbTrans != null)
                {
                    if (result)
                    {
                        dbTrans.Commit();
                    }
                    else
                    {
                        // rollback
                        dbTrans.Rollback();
                    }
                }
                if (dbCon != null && dbCon.State == ConnectionState.Open)
                {
                    dbCon.Close();

                }
            }
            return result;
        }

        /// <summary>
        /// Updates the specified item.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <returns></returns>
        public bool Update(UserPassCode item)
        {
            bool result = true;
            DbConnection dbCon = null;
            DbTransaction dbTrans = null;
            try
            {
                //Get connection name
                string connectionName = ConfigInfo.ConnectionName;

                //Create connection & transaction
                dbCon = Database.CreateConnection(connectionName, true);
                dbTrans = dbCon.BeginTransaction();

                result = UserPassCodeDA.Update(item, connectionName, dbCon, dbTrans) > 0;
            }


            catch (Exception ex)
            {
                LogUtils.Error(string.Format("VISS.Users.UserPassCodeBC.Update({0}, {1}, {2}): \n" + ex.ToString(),
                                    SerializerUtils.JsonSerialize(item)));
                result = false;
            }
            finally
            {
                if (dbTrans != null)
                {
                    if (result)
                    {
                        dbTrans.Commit();
                    }
                    else
                    {
                        // rollback
                        dbTrans.Rollback();
                    }
                }
                if (dbCon != null && dbCon.State == ConnectionState.Open)
                {
                    dbCon.Close();

                }
            }
            return result;
        }

        /// <summary>
        /// Updates the expired date.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <returns></returns>
        public bool UpdateExpiredDate(UserPassCode item)
        {
            bool result = true;
            DbConnection dbCon = null;
            DbTransaction dbTrans = null;
            try
            {
                //Get connection name
                string connectionName = ConfigInfo.ConnectionName;

                //Create connection & transaction
                dbCon = Database.CreateConnection(connectionName, true);
                dbTrans = dbCon.BeginTransaction();

                result = UserPassCodeDA.UpdateExpiredDate(item, connectionName, dbCon, dbTrans) > 0;
            }

            catch (Exception ex)
            {
                LogUtils.Error(string.Format("VISS.Users.UserPassCodeBC.UpdateExpiredDate({0}, {1}, {2}): \n" + ex.ToString(),
                                    SerializerUtils.JsonSerialize(item)));
                result = false;
            }
            finally
            {
                if (dbTrans != null)
                {
                    if (result)
                    {
                        dbTrans.Commit();
                    }
                    else
                    {
                        // rollback
                        dbTrans.Rollback();
                    }
                }
                if (dbCon != null && dbCon.State == ConnectionState.Open)
                {
                    dbCon.Close();

                }
            }
            return result;
            
        }

        /// <summary>
        /// Gets the by user key.
        /// </summary>
        /// <param name="userKey">The user key.</param>
        /// <returns>
        /// return UserEnt if exists, otherwise return null
        /// </returns>
        public UserPassCode GetByUserKey(string userKey)
        {
            UserPassCode item = null;
            try
            {
                if (ConfigInfo.UseCache)
                {
                }
                else
                {
                    //Get connection name
                    string connectionName = ConfigInfo.ConnectionName;

                    item = UserPassCodeDA.GetByUserKey(userKey, connectionName, null, null);
                }
            }
            catch (System.Exception ex)
            {
                LogUtils.Error(string.Format("VISS.Users.UserBC.GetByUserKey({0}): \n" + ex.ToString(), userKey));
            }
            return item;
        }

        #endregion
    }
}
